﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_BOM_TECHNIC
// * CreateDate    : 2013-11-15 22:17:52
// * Creater       : CodeSmith
// * LastChangeDate: 2013-11-15 22:17:52
// * LastChanger   : 2013-11-15 22:17:52
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Bpt
{
    /// <summary>
    /// BomTechnic
    /// </summary>
    public partial class BomTechnicBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objBomTechnicEntity">Entity</param>
        /// <returns>Entity</returns>
        public static BomTechnicEntity GetBomTechnic(BomTechnicEntity objBomTechnicEntity)
        {
            return DataAccess.SelectSingle<BomTechnicEntity>(objBomTechnicEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objBomTechnicEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertBomTechnic(BomTechnicEntity objBomTechnicEntity)
        {
            return DataAccess.Insert(objBomTechnicEntity);
        }

        public static bool InsertBomTechnic(List<BomTechnicEntity> objBomTechnicEntityList)
        {
            return DataAccess.Insert(objBomTechnicEntityList);
        }

        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objBomTechnicEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateBomTechnic(BomTechnicEntity objBomTechnicEntity)
        {
            return DataAccess.Update(objBomTechnicEntity);
        }

        public static bool UpdateBomTechnic(List<BomTechnicEntity> objBomTechnicEntityList)
        {
            return DataAccess.Update<BomTechnicEntity>(objBomTechnicEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objBomTechnicEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteBomTechnic(BomTechnicEntity objBomTechnicEntity)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    string sql = "delete from BOM_TECHNIC_ITEM where bom_technic_id='{0}'";
                    sql = string.Format(sql, objBomTechnicEntity.Id);
                    broker.ExecuteSQL(sql);
                    DataAccess.Delete(objBomTechnicEntity, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static bool DeleteBomTechnic(List<BomTechnicEntity> objBomTechnicEntityList)
        {
            return DataAccess.Delete<BomTechnicEntity>(objBomTechnicEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetBomTechnicAll()
        {
            return DataAccess.SelectAllDataSet<BomTechnicEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<BomTechnicEntity> GetBomTechnicAllList()
        {
            return DataAccess.SelectAll<BomTechnicEntity>();
        }
        #endregion

        public static DataSet GetAllByCondition(int pageIndex, int pageCount, string condition)
        {
            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from BOM_TECHNIC) as cnt
from BOM_TECHNIC
where id not in (
    select top ({1}*({2}-1)) id from BOM_TECHNIC order by created_date
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageCount, pageCount, pageIndex);
                sql = @"select bt.*,pu1.firstName as CREATED_USER,pu2.firstName as UPDATED_USER
from ({0}) bt
left join BPMDB.dbo.PW_User pu1 on bt.CREATED_BY=pu1.userNo
left join BPMDB.dbo.PW_User pu2 on bt.CREATED_BY=pu2.userNo ";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date
) order by created_date desc";
                string sqlSource = @"select bt.*,pu1.firstName as CREATED_USER,pu2.firstName as UPDATED_USER
from BOM_TECHNIC bt
left join BPMDB.dbo.PW_User pu1 on bt.CREATED_BY=pu1.userNo
left join BPMDB.dbo.PW_User pu2 on bt.CREATED_BY=pu2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageCount, pageCount, pageIndex, sqlSource);
            }
            return DataAccess.SelectDataSet(sql);
        }

        public static DataSet GetAllDsBySql(string sql)
        {
            return DataAccess.SelectDataSet(sql);
        }

        public static bool UpdateAndActiveBomTechnic(BomTechnicEntity objBomTechnicEntity)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    var technics = DataAccess.Select<BomTechnicEntity>(new BomTechnicEntity { ProductId = objBomTechnicEntity.ProductId });
                    if (technics == null)
                        return false;
                    foreach (var t in technics)
                    {
                        if (t.Id != objBomTechnicEntity.Id)
                            t.IsActive = 0;
                        else
                        {
                            t.UpdatedBy = objBomTechnicEntity.UpdatedBy;
                            t.UpdatedDate = objBomTechnicEntity.UpdatedDate;
                            t.IsActive = 1;
                            t.ProductId = objBomTechnicEntity.ProductId;
                            t.Ver = objBomTechnicEntity.Ver;
                            t.Description = objBomTechnicEntity.Description;
                            t.Status = objBomTechnicEntity.Status;
                        }
                    }
                    DataAccess.Update(technics, broker);
                    broker.Commit();
                   return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }


        //删除工艺路线及其所有明细
        public static bool DeleteBomTechnicAndDetail(BomTechnicEntity objBomTechnicEntity)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    string sql = "delete from BOM_TECHNIC_ITEM_Mtrl where bom_technic_id='{0}';";
                    sql += "delete from BOM_TECHNIC_ITEM where bom_technic_id='{0}';";
                    sql += "delete from BOM_TECHNIC where ID='{0}';";
                    sql = string.Format(sql, objBomTechnicEntity.Id);
                    broker.ExecuteSQL(sql);
                    DataAccess.Delete(objBomTechnicEntity, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }


        public static List<BomTechnicEntity> GetBomTechnicByLogicNo(string logicNo)
        {
           return  DataAccess.Select<BomTechnicEntity>(new BomTechnicEntity { LogicNo = logicNo });
        }


        public static bool UpdateBomTechnicWithTransaction(List<BomTechnicEntity> routeList)
        {
            List<BomTechnicEntity> deleteRouteMaster = DataAccess.Select(new BomTechnicEntity { LogicNo = routeList[0].LogicNo });


            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (deleteRouteMaster != null && deleteRouteMaster.Count > 0) DataAccess.Delete(deleteRouteMaster, broker);
                    if (routeList != null && routeList.Count > 0) DataAccess.Insert(routeList, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
    }


}

